package org.springboot.base.base;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springboot.base.MyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author ZJL
 * @DateTime 2019年2月22日 下午12:26:30
 * 全局异常处理类
 **/
@ControllerAdvice
public class GlobalExceptionHandler {
	Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
	
	public static final String DEFAULT_ERROR_VIEW = "freemarker/error1";
	
	//返回url地址
    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("exception", e);
        mv.addObject("url", req.getRequestURL());
        mv.setViewName(DEFAULT_ERROR_VIEW);
        return mv;
    }
    
    //返回json数据
    @ExceptionHandler(value = MyException.class)
    @ResponseBody
    public ResultDO<Object> myErrorHandler(HttpServletRequest req, Exception e) throws Exception {
    	ResultDO<Object> result = new ResultDO<Object>();
    	result.setRtnCode(ResultDO.RTN_CODE_ERROR);
    	result.setRtnMsg(e.getMessage());
    	result.setUrl(req.getRequestURL().toString());
    	result.setData("我是自定义的错误处理！2222222222");
        return result;
    }
}
